package com.yfbao.horizon.sacn;

import java.io.*;


public class ClassFileloader extends ClassLoader {
    public static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ClassFileloader.class);
    private final String fileSuffix = ".class";
    private String classLocation;

    public ClassFileloader(String classLocation) {
        super();
        this.classLocation = classLocation;
    }

    public ClassFileloader(ClassLoader parentClassLoader, String classLocation) {
        super(parentClassLoader);
        this.classLocation = classLocation;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] classData = loadClassData(name);
        if(classData ==null || classData.length==0){
            return null;
        }
        return defineClass(name, classData, 0, classData.length);
    }

    private byte[] loadClassFromJarData(String name) {
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        byte[] data = null;
        try {
            File file = new File(classLocation + name.replace(".", "\\") + fileSuffix);
            is = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            int r = 0;
            while (-1 != (r = is.read())) {
                baos.write(r);
            }
            is.close();
            baos.close();
            data = baos.toByteArray();
        } catch (Exception ex) {
            log.error( null, ex);
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException exx) {
                log.error( null, exx);
            }
        }
        return data;
    }

    private byte[] loadClassData(String name) {
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        byte[] data = null;
        try {
            File file = new File(classLocation + name.replace(".", "\\") + fileSuffix);
            if(!file.exists()){
                log.warn("文件不存在："+file.getAbsolutePath());
                return data;
            }
            is = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            int r = 0;
            while (-1 != (r = is.read())) {
                baos.write(r);
            }
            is.close();
            baos.close();
            data = baos.toByteArray();
        } catch (Exception ex) {
            log.error( null, ex);
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException exx) {
                log.error( null, exx);
            }
        }
        return data;
    }

    @Override
    public String toString() {
        return "This is MyClassLoader:" + this.getClass().getSimpleName();
    }

    public static void main(String[] args) throws Exception {
        ClassFileloader myCl = new ClassFileloader(ClassLoader.getSystemClassLoader(), "D:\\workspace\\bug\\ctp-common\\target\\classes\\");
        testClassLoader(myCl);
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    }

    private static void testClassLoader(ClassFileloader cl) throws Exception {
        Class<?> clazz = cl.findClass("com.seeyon.ctp.common.web.filter.RestAuthenticator");
        System.out.println(clazz.getClassLoader().toString());

        Object newObj = clazz.newInstance();
        System.out.println(newObj.toString());
    }
}
